package com.itheima.config;

import com.alibaba.druid.pool.DruidDataSource;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import javax.sql.DataSource;

//加载外部资源配置文件
@PropertySource("classpath:jdbc.properties")
//将dbconfig类设置为spring管理的类
@Component
public class DbConfig {

    @Value("${jdbc.driver}")
    private String driver;
    @Value("${jdbc.url}")
    private String url;
    @Value("${jdbc.username}")
    private String username;
    @Value("${jdbc.password}")
    private String password;

    //配置数据源
    @Bean("druidDataSource")
    public DataSource getDataSource(){
        //创建druid数据库连接池对象
        DruidDataSource dataSource = new DruidDataSource();
        //配置数据库连接的四大特征
        dataSource.setDriverClassName(driver);
        dataSource.setUrl(url);
        dataSource.setUsername(username);
        dataSource.setPassword(password);

        return dataSource;
    }
}
